package com.ccbooks.adapter;

import java.util.List;

import com.ccbooks.bo.BooksBo;
import com.ccbooks.listener.DelListener;
import com.ccbooks.util.BookUtil;
import com.ccbooks.view.BookListView;
import com.ccbooks.view.R;
import com.ccbooks.vo.Book;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BookInfosAdapter extends BaseAdapter {

	public int count;
	private BookListView activity;
	//public List<Book> books;
	private LayoutInflater inflater;

	public BookInfosAdapter(BookListView activity, List<Book> books) {
		this.activity = activity;
		//this.books = books;
		this.inflater = LayoutInflater.from(activity);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return BooksBo. books.size();

	}

	@Override
	public Book getItem(int position) {
		// TODO Auto-generated method stub
		return BooksBo.books.get(position);

	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		// ȡ�ÿؼ���

		LinearLayout ly = (LinearLayout) inflater.inflate(R.layout.bookinfos,
				null);
		ImageView ivDelIcon= (ImageView) ly.findViewById(R.id.ivDelIcon);
		ImageView ivBookIcon = (ImageView) ly.findViewById(R.id.ivBookIcon);
		TextView tvBookName = (TextView) ly.findViewById(R.id.tvBookName);
		TextView tvAuthor = (TextView) ly.findViewById(R.id.tvAuthor);
		TextView tvCategory = (TextView) ly.findViewById(R.id.tvCategory);

		// ��ֵ;
		if (BooksBo.books != null) {
			if(BooksBo.books.get(position).imgpath!=null){
				Bitmap bitmap = BitmapFactory
					.decodeFile(BooksBo.books.get(position).imgpath);
				ivBookIcon.setImageBitmap(bitmap);
			}else{
				ivBookIcon.setImageDrawable(activity.getResources().getDrawable(R.drawable.def_cover_list));
			}
			DelListener delListener=new DelListener(activity,this,BooksBo.books.get(position));
			ivDelIcon.setOnClickListener(delListener);
			tvBookName.setText(BooksBo.books.get(position).bookname);
			if(BooksBo.books.get(position).author !=null && !"".equals( BooksBo.books.get(position).author)){
				tvAuthor.setText(BooksBo.books.get(position).author);
			}else {
				tvAuthor.setText("作者：Unkown");
			}
			if(BooksBo.books.get(position).category !=null && !"".equals( BooksBo.books.get(position).category)){
				tvCategory.setText(BooksBo.books.get(position).category);
			}else {
				tvAuthor.setText("CCBOOKS");
			}
			if(activity.isEdit){
				ivDelIcon.setVisibility(View.VISIBLE);
			}else{
				ivDelIcon.setVisibility(View.GONE);
			}
			//activity.ivs.add(ivBookIcon);
		}
		return ly;
	}

}
